/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.table;

import com.jspx.boot.sign.AuditingType;
import com.jspx.boot.sign.CongealType;
import com.jspx.boot.sign.UserType;
import com.jspx.boot.sign.YesNoType;
import com.jspx.json.JSONArray;
import com.jspx.json.JSONObject;
import com.jspx.json.NotExpose;
import com.jspx.sober.annotation.*;
import com.jspx.sober.table.OperateTable;
import com.jspx.txweb.IRole;
import com.jspx.txweb.util.TXWebUtil;
import com.jspx.utils.ArrayUtil;
import com.jspx.utils.DateUtil;
import com.jspx.utils.FileSuffixUtil;
import com.jspx.utils.StringUtil;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2010-7-25
 * Time: 22:10:00
 * com.jspx.txweb.table.Role
 */
@Table(name = "jspx_role", caption = "默认角色", cache = true, cacheName = "roleCache")
public class Role extends OperateTable implements IRole {
    public static final String roleNamespace = ":";
    public static final String roleMethod = "_";


    @Id(type = IDType.serial)
    @Column(caption = "ID", notNull = true)
    private int id = 0;

    //角色名称，也可以是部门名称
    @Column(caption = "角色名称", length = 50, notNull = true)
    private String name = StringUtil.empty;

    @NotExpose
    @Column(caption = "拼音", length = 100, hidden = true, notNull = true)
    private String spelling = StringUtil.empty;

    @Column(caption = "描述", length = 250)
    private String description = StringUtil.empty;

    @Column(caption = "用户类型", length = 2)
    private int userType = UserType.NONE;

    @Column(caption = "图片", length = 250, notNull = true)
    private String images = StringUtil.empty;

    @Column(caption = "办公角色", length = 2)
    private int officeType = YesNoType.NO;

    //是否允许上传
    @Column(caption = "是否允许上传", length = 2)
    private int useUpload = 0;

    @Column(caption = "上传的文件大小", notNull = true,defaultValue = "0")
    private int uploadSize = 10240;

    @Column(caption = "上传的图片大小", notNull = true,defaultValue = "0")
    private int uploadImageSize = 1024;

    @Column(caption = "上传的视频大小", notNull = true,defaultValue = "0")
    private int uploadVideoSize = 102400;

    //上传的文件类型
    @Column(caption = "上传的文件类型", length = 250, notNull = true)
    private String uploadFileTypes = "*";

    @Column(caption = "磁盘空间", notNull = true,defaultValue = "0")
    private long diskSize = 102400;

    @Column(caption = "FTP共享目录", length = 250, notNull = true)
    private String uploadFolder = StringUtil.empty;

    @Column(caption = "冻结", length = 2, notNull = true)
    private int congealType = CongealType.NO_CONGEAL;

    @Column(caption = "冻结时间", notNull = true)
    private Date congealDate = DateUtil.empty;

    @Column(caption = "审核", length = 2, notNull = true)
    private int auditingType = AuditingType.OK;

    //rwde  读 写 删 执行  ftp 情况使用 主要留给FTP空间使用
    @Column(caption = "目录权限", length = 20, notNull = true)
    private String permission = "rw-d-";

    @Column(caption = "动作列表", length = 3000, notNull = false)
    private String operates = StringUtil.empty;
    //数据保存格式，一行一个，开始第一个参数为命名空间，然后文件名称部分为文件名称(包含通配).采用base64加密后作为id使用分割符号：后边为执行方法
    //例如： 命名空间/base64:method   注意，这里并不保存程序的class信息，采用文件名称来识别class信息
    //jcms/xxx41234:ssd

    //格式[id:name]多个使用;分开
    @Column(caption = "管理者", length = 250, notNull = true)
    private String manager = StringUtil.empty;

    @NotExpose
    @Column(caption = "排序时间", notNull = true)
    private Date sortDate = new Date();

    @NotExpose
    @Column(caption = "命名空间", length = 50, notNull = true)
    private String namespace = StringUtil.empty;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getUserType() {
        return userType;
    }

    public void setUserType(int userType) {
        this.userType = userType;
    }

    public String getImages() {
        if (!StringUtil.hasLength(images)) return "/share/pimg/usertype/0001.gif";
        return images;
    }

    public void setImages(String images) {
        this.images = images;
    }

    public int getUseUpload() {
        return useUpload;
    }

    public void setUseUpload(int useUpload) {
        this.useUpload = useUpload;
    }

    public int getUploadSize() {
        return uploadSize;
    }

    public void setUploadSize(int uploadSize) {
        this.uploadSize = uploadSize;
    }

    public String getUploadFileTypes() {
        return uploadFileTypes;
    }

    public void setUploadFileTypes(String uploadFileTypes) {
        this.uploadFileTypes = uploadFileTypes;
    }

    public long getDiskSize() {
        return diskSize;
    }

    public void setDiskSize(long diskSize) {
        this.diskSize = diskSize;
    }

    public int getCongealType() {
        return congealType;
    }

    public void setCongealType(int congealType) {
        this.congealType = congealType;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public String getManager() {
        return manager;
    }

    public void setManager(String manager) {
        this.manager = manager;
    }


    public int getAuditingType() {
        return auditingType;
    }

    public void setAuditingType(int auditingType) {
        this.auditingType = auditingType;
    }

    public String getUploadFolder() {
        return uploadFolder;
    }

    public void setUploadFolder(String uploadFolder) {
        this.uploadFolder = uploadFolder;
    }

    public String getOperates() {
        return operates;
    }

    public void setOperates(String operates) {
        this.operates = operates;
    }

    public boolean checkOperate(String methodId) {
        return operates != null && methodId != null && (operates.contains(methodId));
    }

    //作为一个缓存，提供运行速度
    private String[] operatesLines = null;

    private String[] getOperatesLines() {
        if (operatesLines == null && !StringUtil.isNULL(operates)) {
            operatesLines = StringUtil.split(StringUtil.replace(operates, StringUtil.CRLF, StringUtil.CR), StringUtil.CR);
            ArrayUtil.sort(operatesLines, "/", true);
        }
        return operatesLines;
    }

    public boolean checkOperate(String actionName, String classMethod) {
        return checkOperate(namespace, actionName, classMethod);
    }

    /**
     * 这里的权限判断也和txweb 的action 配置一样，是有继承关系的,
     * 先判断本级，如果没有，并且本机没有* 的通配,那么就到父级去判断
     * 这种判断方式和TXWeb的继承配置方式一样
     *
     * @param namespace   命名空间
     * @param actionName  页面名称
     * @param classMethod 执行方法
     * @return 判断是否有权限执行
     */
    public boolean checkOperate(String namespace, String actionName, String classMethod) {
        if (congealType == CongealType.YES_CONGEAL) return false;
        if (userType >= UserType.ADMINISTRATOR) return true;
        if (StringUtil.isNULL(operates)) return false;
        if (!StringUtil.hasLength(classMethod)) classMethod = TXWebUtil.defaultExecute;
        operatesLines = getOperatesLines();
        for (String line : operatesLines) {
            String oNamespace = StringUtil.trim(StringUtil.substringBefore(line, roleNamespace));
            if (oNamespace != null && !namespace.startsWith(oNamespace)) continue;
            String oActionName = TXWebUtil.getOperateActionName(line);
            if (!StringUtil.hasLength(oActionName)) continue;
            if (!oActionName.equals("*") && !actionName.matches(oActionName)) continue;
            if (line.endsWith(roleMethod + classMethod))
            {
                return true;
            }
        }
        return false;

    }

    public boolean isOperateConfig(String namespace, String actionName, String classMethod) {
        operatesLines = getOperatesLines();
        return ArrayUtil.inArray(operatesLines, TXWebUtil.getOperateMethodId(namespace, actionName, classMethod), true);
    }

    public boolean isOperateConfig(String actionMethodId)
    {
        operatesLines = getOperatesLines();
        return ArrayUtil.inArray(operatesLines, actionMethodId, false);
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    public int getOfficeType() {
        return officeType;
    }

    public void setOfficeType(int officeType) {
        this.officeType = officeType;
    }

    public Date getSortDate() {
        return sortDate;
    }

    public void setSortDate(Date sortDate) {
        this.sortDate = sortDate;
    }

    public Date getCongealDate() {
        return congealDate;
    }

    public void setCongealDate(Date congealDate) {
        this.congealDate = congealDate;
    }

    public String getSpelling() {
        return spelling;
    }

    public void setSpelling(String spelling) {
        this.spelling = spelling;
    }

    public int getUploadImageSize()
    {
        return uploadImageSize==0?1024:uploadImageSize;
    }

    public void setUploadImageSize(int uploadImageSize) {
        this.uploadImageSize = uploadImageSize;
    }

    public int getUploadVideoSize()
    {
        return uploadVideoSize==0?10240:uploadVideoSize;
    }

    public void setUploadVideoSize(int uploadVideoSize) {
        this.uploadVideoSize = uploadVideoSize;
    }

    public String getJsonUploadTypes(String type)
    {
        String[] uploadTypes = new String[0];
        if ("*".equalsIgnoreCase(uploadFileTypes)) {
            uploadTypes = ArrayUtil.join(uploadTypes, FileSuffixUtil.imageTypes);
            uploadTypes = ArrayUtil.join(uploadTypes,FileSuffixUtil.zipTypes);
            uploadTypes = ArrayUtil.join(uploadTypes,FileSuffixUtil.videoTypes);
            uploadTypes = ArrayUtil.join(uploadTypes,FileSuffixUtil.officeTypes);
        }
        else
            uploadTypes = StringUtil.split(StringUtil.replace(uploadFileTypes, StringUtil.COMMAS, StringUtil.SEMICOLON), StringUtil.SEMICOLON);

        JSONArray json = new JSONArray();
        if ("*".equalsIgnoreCase(type)||"Image".equalsIgnoreCase(type)) {
            JSONObject typeJson = new JSONObject();
            typeJson.put("title", "Image files");

            StringBuilder types = new StringBuilder();
            for (String t:uploadTypes) {
                if (ArrayUtil.inArray(FileSuffixUtil.imageTypes, t, true)) {
                    types.append(t).append(StringUtil.COMMAS);
                    uploadTypes = ArrayUtil.delete(uploadTypes,t,true);
                }
            }
            if (types.length() > 0) {
                if (types.toString().endsWith(StringUtil.COMMAS)) types.setLength(types.length() - 1);
                typeJson.put("extensions", types.toString());
                json.put(typeJson);
            }
        }
        if ("*".equalsIgnoreCase(type)||"Zip".equalsIgnoreCase(type)) {
            JSONObject typeJson = new JSONObject();
            typeJson.put("title", "Zip files");

            StringBuilder types = new StringBuilder();
            for (String t : uploadTypes) {
                if (ArrayUtil.inArray(FileSuffixUtil.zipTypes, t, true)) {
                    types.append(t).append(StringUtil.COMMAS);
                    uploadTypes = ArrayUtil.delete(uploadTypes,t,true);
                }
            }
            if (types.length() > 0) {
                if (types.toString().endsWith(StringUtil.COMMAS)) types.setLength(types.length() - 1);
                typeJson.put("extensions", types.toString());
                json.put(typeJson);
            }
        }

        if ("*".equalsIgnoreCase(type)||"Video".equalsIgnoreCase(type)) {
            JSONObject typeJson = new JSONObject();
            typeJson.put("title", "Video files");

            StringBuilder types = new StringBuilder();
            for (String t : uploadTypes) {
                if (ArrayUtil.inArray(FileSuffixUtil.videoTypes, t, true)) {
                    types.append(t).append(StringUtil.COMMAS);
                    uploadTypes = ArrayUtil.delete(uploadTypes,t,true);
                }
            }
            if (types.length() >0) {
                if (types.toString().endsWith(StringUtil.COMMAS)) types.setLength(types.length() - 1);
                typeJson.put("extensions", types.toString());
                json.put(typeJson);
            }
        }


        if ("*".equalsIgnoreCase(type)||"Office".equalsIgnoreCase(type)) {
            JSONObject typeJson = new JSONObject();
            typeJson.put("title", "Office files");

            StringBuilder types = new StringBuilder();
            for (String t : uploadTypes) {
                if (ArrayUtil.inArray(FileSuffixUtil.officeTypes, t, true)) {
                    types.append(t).append(StringUtil.COMMAS);
                    uploadTypes = ArrayUtil.delete(uploadTypes,t,true);
                }
            }
            if (types.length() > 0) {
                if (types.toString().endsWith(StringUtil.COMMAS)) types.setLength(types.length() - 1);
                typeJson.put("extensions", types.toString());
                json.put(typeJson);
            }
        }

        if (("*".equalsIgnoreCase(type)||"Other".equalsIgnoreCase(type))&&!ArrayUtil.isEmpty(uploadTypes) )
        {
            JSONObject typeJson = new JSONObject();
            typeJson.put("title", "Other files");
            StringBuilder types = new StringBuilder();
            for (String t : uploadTypes) {
                    types.append(t).append(StringUtil.COMMAS);
            }
            if (types.length() > 0) {
                if (types.toString().endsWith(StringUtil.COMMAS)) types.setLength(types.length() - 1);
                typeJson.put("extensions", types.toString());
                json.put(typeJson);
            }
        }
        return json.toString();
    }


    public String getOptionUploadTypes()
    {
        return getOptionUploadTypes(true);
    }

    public String getOptionUploadTypes(boolean cut)
    {
        String[] uploadTypes = new String[0];
        if ("*".equalsIgnoreCase(uploadFileTypes)) {
            uploadTypes = ArrayUtil.join(uploadTypes, FileSuffixUtil.imageTypes);
            uploadTypes = ArrayUtil.join(uploadTypes,FileSuffixUtil.zipTypes);
            uploadTypes = ArrayUtil.join(uploadTypes,FileSuffixUtil.videoTypes);
            uploadTypes = ArrayUtil.join(uploadTypes,FileSuffixUtil.officeTypes);
        }
        else
            uploadTypes = StringUtil.split(StringUtil.replace(uploadFileTypes, StringUtil.COMMAS, StringUtil.SEMICOLON), StringUtil.SEMICOLON);

        final String type  = uploadFileTypes;
        int i=0;
        StringBuilder option = new StringBuilder();
        if ("*".equalsIgnoreCase(type)||"Image".equalsIgnoreCase(type)) {
            option.append("Image File").append(StringUtil.COLON);
            StringBuilder types = new StringBuilder();
            for (String t:uploadTypes) {
                if (ArrayUtil.inArray(FileSuffixUtil.imageTypes, t, true)) {
                    types.append(t).append(StringUtil.COMMAS);
                    uploadTypes = ArrayUtil.delete(uploadTypes,t,true);
                    i++;
                    if (cut&&i>15)
                    {
                        types.append("...").append(StringUtil.COMMAS);
                        break;
                    }
                }
            }
            if (types.length() > 0) {
                if (types.toString().endsWith(StringUtil.COMMAS)) types.setLength(types.length() - 1);
                option.append(types.toString()).append(StringUtil.SEMICOLON);
            }
        }
        if ("*".equalsIgnoreCase(type)||"Zip".equalsIgnoreCase(type)) {
            option.append("Zip File").append(StringUtil.COLON);
            i=0;
            StringBuilder types = new StringBuilder();
            for (String t : uploadTypes) {
                if (ArrayUtil.inArray(FileSuffixUtil.zipTypes, t, true)) {
                    types.append(t).append(StringUtil.COMMAS);
                    uploadTypes = ArrayUtil.delete(uploadTypes,t,true);
                    i++;
                    if (cut&&i>15)
                    {
                        types.append("...").append(StringUtil.COMMAS);
                        break;
                    }

                }
            }
            if (types.length() > 0) {
                if (types.toString().endsWith(StringUtil.COMMAS)) types.setLength(types.length() - 1);
                option.append(types.toString()).append(StringUtil.SEMICOLON);
            }
        }
        if ("*".equalsIgnoreCase(type)||"Video".equalsIgnoreCase(type)) {
            option.append("Video File").append(StringUtil.COLON);
            i=0;
            StringBuilder types = new StringBuilder();
            for (String t : uploadTypes) {
                if (ArrayUtil.inArray(FileSuffixUtil.videoTypes, t, true)) {
                    types.append(t).append(StringUtil.COMMAS);
                    uploadTypes = ArrayUtil.delete(uploadTypes,t,true);
                    i++;
                    if (cut&&i>15)
                    {
                        types.append("...").append(StringUtil.COMMAS);
                        break;
                    }

                }
            }
            if (types.length() > 0) {
                if (types.toString().endsWith(StringUtil.COMMAS)) types.setLength(types.length() - 1);
                option.append(types.toString()).append(StringUtil.SEMICOLON);
            }
        }
        if ("*".equalsIgnoreCase(type)||"Office".equalsIgnoreCase(type)) {
            option.append("Office File").append(StringUtil.COLON);
            i=0;
            StringBuilder types = new StringBuilder();
            for (String t : uploadTypes) {
                if (ArrayUtil.inArray(FileSuffixUtil.officeTypes, t, true)) {
                    types.append(t).append(StringUtil.COMMAS);
                    uploadTypes = ArrayUtil.delete(uploadTypes,t,true);
                    i++;
                    if (cut&&i>15)
                    {
                        types.append("...").append(StringUtil.COMMAS);
                        break;
                    }
                }
            }
            if (types.length() > 0) {
                if (types.toString().endsWith(StringUtil.COMMAS)) types.setLength(types.length() - 1);
                option.append(types.toString()).append(StringUtil.SEMICOLON);
            }
        }
        if (("*".equalsIgnoreCase(type)||"Other".equalsIgnoreCase(type))&&!ArrayUtil.isEmpty(uploadTypes) )
        {
            option.append("Other File").append(StringUtil.COLON);
            i=0;
            StringBuilder types = new StringBuilder();
            for (String t : uploadTypes) {
                types.append(t).append(StringUtil.COMMAS);
                i++;
                if (cut&&i>15)
                {
                    types.append("...").append(StringUtil.COMMAS);
                    break;
                }
            }
            if (types.length() > 0) {
                if (types.toString().endsWith(StringUtil.COMMAS)) types.setLength(types.length() - 1);
                option.append(types.toString()).append(StringUtil.SEMICOLON);
            }
        }
        return option.toString();
    }

}